/**
 * 
 */
package com.yidu.menuProject.common;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
/**
 * 类的描述--工具类
 * @author sylval
 * @date 2018年1月5日 上午9:43:40
 * @version 1.0
 */
public class Tools {
	public static boolean isEmpty(String str){
		if(str==null){
			return true;
		}
		if("".equals(str)){
			return true;
		}
		return false;
	}
	/**
	 * @param response
	 * @param obj
	 * @throws IOException
	 */
	/*public static void write(HttpServletResponse response,Object obj) throws IOException{
		PrintWriter out = response.getWriter();
		Gson gson = new Gson();
		String json = gson.toJson(obj);
		out.write(json);
		System.out.println("-------"+json);
		out.close();
	}*/
	
	/**
	 * 转换格式
	 * @param str
	 * @return
	 */
	public static Integer stringToInt(String str){
		if(isEmpty(str)) return null;
		return Integer.valueOf(str);
	}
	public static Date stringToData(String str) throws ParseException{
		if(isEmpty(str)) return null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.parse(str);
	}
	public static String DateToString(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(date);
	}
	
	/**
	 * 获得随机数
	 * @param length
	 * @return
	 */
	public static String getStringRandom(int length) {  
        
        String val = "";  
        Random random = new Random();  
        for(int i = 0; i < length; i++) {  
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";  
            if( "char".equalsIgnoreCase(charOrNum) ) {  
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;  
                val += (char)(random.nextInt(26) + temp);  
            } else if( "num".equalsIgnoreCase(charOrNum) ) {  
                val += String.valueOf(random.nextInt(10));  
            }  
        }  
        return val;  
    }  
}
